<template>
  <div style="margin-top: 20px; margin: 50px; margin-right: 100px">
    <!-- 按钮 -->

    <el-row>
      <el-button
        style="float: right"
        type="primary"
        @click="dialogFormVisible = true; estate={}"
      >+ 新增小区</el-button>
    </el-row>
    <br>
    <!-- 数据表格 -->
    <template>
      <el-table
        ref="multipleTable"
        highlight-current-row
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        border
      >
        <el-table-column type="index" width="100" label="序号" header-align="center" align="center" />
        <el-table-column prop="name" label="小区名称" header-align="center" align="center" />
        <el-table-column label="最后操作时间" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row.updateTime ? scope.row.updateTime.replace('T',' '):'' }}
          </template>
        </el-table-column>

        <el-table-column label="操作" width="420" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              plain
              @click="selectById(scope.row.id)"
            >编辑</el-button>

            <el-button
              size="mini"
              type="danger"
              plain
              @click="deleteById(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <!-- 新建对话框 -->

    <el-dialog title="保存小区" :visible.sync="dialogFormVisible">
      <el-form :model="estate">
        <el-form-item label="小区名称" :label-width="formLabelWidth">
          <el-input v-model="estate.name" placeholder="请输入小区名称" autocomplete="off" style="width:150px" />
          建筑年代
            <el-date-picker
              v-model="estate.year"
              default-value="2000"
              value-format="yyyy"
              clearable
              type="year"
              placeholder="选择日期"
              size="small"
            />
        </el-form-item>
        <el-form-item label="小区详情" :label-width="formLabelWidth">
          <el-input v-model="estate.info" show-word-limit type="textarea" rows="10" placeholder="请输入小区详情" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { page, add, update, deleteById, selectById } from '@/api/estate.js'
import { getToken } from '@/utils/auth'

export default {
  data() {
    return {
      background: true,
      // 每页显示的条数
      pageSize: 5,
      // 总记录数
      totalCount: 100,
      // 当前页码
      currentPage: 1,
      // 添加数据对话框是否展示的标记
      dialogVisible: false,
      // 品牌模型数据
      searchEstate: {
        name: ''
      },
      formLabelWidth: '120px',
      dialogFormVisible: false, // 控制对话框是否可见
      estate: {
        name: ''
      },
      token: { token: getToken() },
      tableData: []
    }
  },
  mounted() {
    this.page() // 当页面加载完成后，发送异步请求，获取数据
  },

  methods: {
    // 查询分页数据
    page() {
      page(
        this.currentPage,
        this.pageSize
      ).then((res) => {
        this.totalCount = res.data.data.total
        this.tableData = res.data.data.rows
      })
    },
    // 保存方法
    add() {
      let method // 添加
      if (this.estate.id) {
        method = update(this.estate) // 修改
      } else {
        method = add(this.estate) // 添加
      }
      method.then((result) => {
        if (result.data.code === 1) {
          // 修改成功
          this.$message.success('恭喜你，保存成功')
          // 重新查询数据
          this.page()
        } else {
          this.$message.error(result.data.msg)
        }
      })
      // 关闭新建窗口
      this.dialogFormVisible = false

      // 清空模型数据
      this.estate = {}
    },

    // 删除小区
    deleteById(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteById(id).then((result) => {
          if (result.data.code == 1) {
            this.$message({
              message: '恭喜你，删除成功',
              type: 'success'
            })
          } else {
            this.$message.error(result.data.msg)
          }
          // 重新查询数据
          this.page()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },

    // 根据ID查询小区 -- 回显
    selectById(id) {
      this.dialogFormVisible = true
      selectById(id).then((result) => {
        this.estate = result.data.data
      })
    },
  }
}
</script>
<style>

</style>
